package top.hidoctor.auscultation.feature.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tang.net.NetClient;
import com.tang.util.AppUtil;
import com.tang.util.Event;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Arrays;
import java.util.List;

import top.hidoctor.auscultation.R;
import top.hidoctor.auscultation.adapter.XinyinAdapter;
import top.hidoctor.auscultation.base.BaseActivity;
import top.hidoctor.auscultation.bean.SampleBean;
import top.hidoctor.auscultation.bean.ShaixuanBean;
import top.hidoctor.auscultation.event.AddYangbenEvent;
import top.hidoctor.auscultation.net.c.SampleListContract;
import top.hidoctor.auscultation.net.p.SampleListPresenter;
import top.hidoctor.auscultation.net.req.SampleReq;
import top.hidoctor.auscultation.widget.Player;

public class MyXyListActivity extends BaseActivity implements SampleListContract.View, View.OnClickListener {

    private RecyclerView recycler;
    private TextView tvShaixuan;
    private SwipeRefreshLayout refreshLayout;
    private DrawerLayout drawer;
    private SampleReq req;
    private SampleListPresenter presenter;
    private XinyinAdapter adapter;
    private EditText editSearch;
    private Button btnChongzhi, btnConfirm;
    private RadioButton rb_bz1, rb_bz2, rb_bz3, rb_sh1, rb_sh2, rb_sh3, rb_yb1, rb_yb2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_my_xy_list);
        EventBus.getDefault().register(this);
        presenter = new SampleListPresenter(this, this);
        req = new SampleReq();
        initView();
    }

    private void initView() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        TextView tvTitle = findViewById(R.id.tv_title);
        TextView tvSetting = findViewById(R.id.tv_setting);
        toolbar.setNavigationIcon(R.mipmap.ic_back_left);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        tvTitle.setText("我的心音样本");
        tvSetting.setText("+ 新增");
        editSearch = findViewById(R.id.edit_search);
        recycler = findViewById(R.id.recycler);
        tvShaixuan = findViewById(R.id.tv_shaixuan);
        refreshLayout = findViewById(R.id.refreshLayout);
        drawer = findViewById(R.id.drawer);
        recycler.setLayoutManager(new LinearLayoutManager(this));
        recycler.setAdapter(adapter = new XinyinAdapter(R.layout.item_xinyin, null));
        View view = LayoutInflater.from(this).inflate(R.layout.layout_empty_list, (ViewGroup) recycler.getParent(), false);
        adapter.setEmptyView(view);
        adapter.isFirstOnly(false);
        adapter.openLoadAnimation();
        adapter.openLoadMore(10, true);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadMore();
            }
        });
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Player currentPlayer = adapter.getCurrentPlayer();
                if (currentPlayer != null) {
                    currentPlayer.stop();
                }
                hideSoft(recycler);
                req.setKeyWord(editSearch.getText().toString().trim());
                refresh();
            }
        });
        editSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                //以下方法防止两次发送请求
                if (actionId == EditorInfo.IME_ACTION_SEND ||
                        (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    switch (event.getAction()) {
                        case KeyEvent.ACTION_UP:
                            //发送请求
                            req.setKeyWord(v.getText().toString().trim());
                            refresh();
                            return true;
                        default:
                            return true;
                    }
                }
                return false;
            }
        });

        tvSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MyXyListActivity.this, AddYangbenActivity.class));
            }
        });

        initDraw();
        refresh();
    }

    private void initDraw() {

        RadioGroup rgBz = findViewById(R.id.rg_bz);
        rb_bz1 = findViewById(R.id.rb_bz1);
        rb_bz2 = findViewById(R.id.rb_bz2);
        rb_bz3 = findViewById(R.id.rb_bz3);

        RadioGroup rgSh = findViewById(R.id.rg_sh);
        rb_sh1 = findViewById(R.id.rb_sh1);
        rb_sh2 = findViewById(R.id.rb_sh2);
        rb_sh3 = findViewById(R.id.rb_sh3);

        RadioGroup rgYb = findViewById(R.id.rg_yb);
        rb_yb1 = findViewById(R.id.rb_yb1);
        rb_yb2 = findViewById(R.id.rb_yb2);
        btnChongzhi = findViewById(R.id.btn_chongzhi);
        btnConfirm = findViewById(R.id.btn_confirm);

        rgBz.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_bz1:
                        req.setLabelStatus("0");
                        break;
                    case R.id.rb_bz2:
                        req.setLabelStatus("10");
                        break;
                    case R.id.rb_bz3:
                        req.setLabelStatus("20");
                        break;
                    default:
                        break;
                }
            }
        });

        rgSh.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_sh1:
                        req.setAuditStatus("0");
                        break;
                    case R.id.rb_sh2:
                        req.setAuditStatus("10");
                        break;
                    case R.id.rb_sh3:
                        req.setAuditStatus("20");
                        break;
                    default:
                        break;
                }
            }
        });

        rgYb.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_yb1:
                        req.setStatus("10");
                        break;
                    case R.id.rb_yb2:
                        req.setStatus("20");
                        break;
                    default:
                        break;
                }
            }
        });
        btnChongzhi.setOnClickListener(this);
        btnConfirm.setOnClickListener(this);
        tvShaixuan.setOnClickListener(this);
    }

    /**
     * 上啦加载
     */
    private void loadMore() {
        req.setPageNo(1 + req.getPageNo());
        presenter.getList(req, refreshLayout);
    }

    /**
     * 下啦刷新
     */
    private void refresh() {
        req.setPageNo(1);
        presenter.getList(req, refreshLayout);
    }

    /**
     * 获取列表数据
     * @param bean
     */
    @Override
    public void getListSuc(SampleBean bean) {
        if (req.getPageNo() == 1) {
            refreshLayout.setRefreshing(false);
            adapter.setNewData(bean.getResult());
        } else {
            adapter.addData(bean.getResult());
        }
        adapter.openLoadMore(bean.getTotalItems() > adapter.getItemCount());
    }

    /**
     * 点击事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_shaixuan:
                drawer.openDrawer(Gravity.RIGHT);
                break;
            case R.id.btn_chongzhi:
                drawer.closeDrawer(Gravity.RIGHT);
                rb_bz1.setChecked(false);
                rb_bz2.setChecked(false);
                rb_bz3.setChecked(false);
                rb_sh1.setChecked(false);
                rb_sh2.setChecked(false);
                rb_sh3.setChecked(false);
                rb_yb1.setChecked(false);
                rb_yb2.setChecked(false);
                editSearch.setText("");
                req.setKeyWord("");
                req.setStatus("");
                req.setAuditStatus("");
                req.setLabelStatus("");
                refresh();
                break;
            case R.id.btn_confirm:
                drawer.closeDrawer(Gravity.RIGHT);
                editSearch.setText("");
                req.setKeyWord("");
                refresh();
                break;

        }
    }

    @Override
    public void onError(String code, String msg) {
        super.onError(code, msg);
        refreshLayout.setRefreshing(false);
        showToast(msg);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Player currentPlayer = adapter.getCurrentPlayer();
        if (currentPlayer != null) {
            currentPlayer.stop();
        }
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(AddYangbenEvent event) {
        refresh();
    }
}
